home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Clock / Sources / ClockFra.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  6.7 KB  |  252 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                ClockFra.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Lonnie Millett
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef CLOCKFRA_H
  14. #include "ClockFra.h"
  15. #endif
  16.  
  17. #ifndef CLOCKFAC_H
  18. #include "ClockFac.h"
  19. #endif
  20.  
  21. #ifndef CLOCKPAR_H
  22. #include "ClockPar.h"
  23. #endif
  24.  
  25. // ----- FrameWork Includes -----
  26.  
  27. #ifndef FWUTIL_H
  28. #include <FWUtil.h>
  29. #endif
  30.  
  31. // ----- Graphic Includes -----
  32.  
  33. #ifndef FWTXTSHP_H
  34. #include <FWTxtShp.h>
  35. #endif
  36.  
  37. // ----- OpenDoc Includes -----
  38.  
  39. #ifndef _FRAME_
  40. #include <Frame.h>
  41. #endif
  42.  
  43. #ifndef _SHAPE_
  44. #include <Shape.h>
  45. #endif
  46.  
  47. #ifndef _WINDOW_
  48. #include <Window.h>
  49. #endif
  50.  
  51. #ifndef _MENUBAR_
  52. #include <MenuBar.h>
  53. #endif
  54.  
  55. #ifndef _XMPSESSN_
  56. #include <XMPSessM.h>
  57. #endif
  58.  
  59. #ifndef _ARBITRAT_
  60. #include <Arbitrat.h>
  61. #endif
  62.  
  63. // ----- Macintosh Includes -----
  64.  
  65. #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
  66. #include <Quickdraw.h>
  67. #endif
  68.  
  69. #if defined(FW_BUILD_MAC) && !defined(__FONTS__)
  70. #include <Fonts.h>
  71. #endif
  72.  
  73. #if defined(FW_BUILD_MAC) && !defined(__TEXTEDIT__)
  74. #include <TextEdit.h>
  75. #endif
  76.  
  77. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  78. #include <Script.h>
  79. #endif
  80.  
  81. #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
  82. #include <TextUtils.h>
  83. #endif
  84.  
  85. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  86. #include <math routines.h>
  87. #endif
  88.  
  89. #pragma segment cclockframe
  90.  
  91. //==============================================================================
  92. // Constants
  93. //==============================================================================
  94.  
  95. const short kClockMinimumSize = 120;
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // CClockFrame::CClockFrame
  99. //----------------------------------------------------------------------------------------
  100. CClockFrame::CClockFrame()
  101.                       : FW_CFrame()
  102. {
  103.     fClockPart = NULL;
  104. }
  105.  
  106. //----------------------------------------------------------------------------------------
  107. // CClockFrame::~CClockFrame
  108. //----------------------------------------------------------------------------------------
  109. CClockFrame::~CClockFrame()
  110. {
  111. }
  112.  
  113. //----------------------------------------------------------------------------------------
  114. // CClockFrame::InitClockFrame
  115. //----------------------------------------------------------------------------------------
  116. void CClockFrame::InitClockFrame(XMPFrame* xmpFrame, CClockPart* clockPart)
  117. {
  118.     this->InitFrame(xmpFrame, clockPart);
  119.     
  120.     // ----- By default we put all of them -----
  121.     AddToFocusSet(clockPart->GetMenuFocusToken());
  122.     AddToFocusSet(clockPart->GetSelectionFocusToken());
  123.  
  124.     fClockPart = clockPart;
  125.     
  126.     // Calculate a default rectangle size for a digital clock
  127. /*    Str255 digitalWidth;
  128.     {
  129.         FW_CAcquireASLMResourceAccess aq;
  130.         GetIndString(digitalWidth, kClockPartStrings, kClockDigitalWidthString);
  131.     }
  132.     
  133.     FW_CString32 tempStr;
  134.     tempStr.ReplaceAll(digitalWidth);
  135. */
  136.     FW_CString32 tempStr("12:59:59 MM");
  137.  
  138.     FW_CTextShape textShape(tempStr, FW_kZeroPoint, FW_kAscentLine);
  139.     textShape->SetFontName(FW_kSystemFont);
  140.     textShape->SetFontSize(ff(12));    
  141.     fDigitalClockRect = textShape->GetShapeBounds();
  142.     fDigitalClockRect.Inset(ff(-3), ff(-3));
  143.     fDigitalClockRect.Place(0,0);
  144.     
  145.     // ----- Set as Droppable -----
  146.     this->SetDroppable(TRUE);
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // CClockFrame::GetNewFrameShape
  151. //----------------------------------------------------------------------------------------
  152. XMPShape* CClockFrame::GetNewFrameShape(FW_CFacet* facet)
  153. {
  154.     FW_CGraphicContext gc(facet->GetXMPFacet());
  155.  
  156.     XMPRgnHandle frameRegion = NewRgn();
  157.     FW_SPlatformRect frameRect;
  158.  
  159.     XMPShape* newFrameShape = ::NewXMPShape();
  160.  
  161.     if (fClockPart->GetClockType() == kAnalogClock)
  162.     {
  163.         XMPRgnHandle shapeRegion = this->GetXMPFrame()->GetFrameShape()->GetQDRegion();
  164.         short size = (*shapeRegion)->rgnBBox.bottom - (*shapeRegion)->rgnBBox.top;
  165.         if (size > (*shapeRegion)->rgnBBox.right - (*shapeRegion)->rgnBBox.left)
  166.             size = (*shapeRegion)->rgnBBox.right - (*shapeRegion)->rgnBBox.left;
  167.         
  168.         if (size < kClockMinimumSize)
  169.             size = kClockMinimumSize;
  170.         
  171.         SetRect(&frameRect, 0, 0, size, size);
  172.         OpenRgn();
  173.         FrameOval(&frameRect);
  174.         CloseRgn(frameRegion);
  175.         newFrameShape->SetQDRegion(frameRegion);
  176.     }
  177.     else
  178.     {
  179.         newFrameShape->SetRectangle(&fDigitalClockRect);
  180.     }
  181.     
  182.     return newFrameShape;
  183. }
  184.     
  185. //----------------------------------------------------------------------------------------
  186. // CClockFrame::NewFacet
  187. //----------------------------------------------------------------------------------------
  188. FW_CFacet* CClockFrame::NewFacet(XMPFacet* xmpFacet)
  189. {
  190.     CClockFacet* facet = new CClockFacet;
  191.     facet->InitClockFacet(xmpFacet, this);
  192.     return facet;
  193. }
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // CClockFrame::FacetAdded
  197. //----------------------------------------------------------------------------------------
  198. void CClockFrame::FacetAdded(FW_CFacet* facet)
  199. {
  200.     FW_CFrame::FacetAdded(facet);
  201.     
  202.     this->RequestFrameShape(this->GetNewFrameShape(facet));
  203.     this->UpdateUsedAndActiveShapes();
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // CClockFrame::FacetRemoved
  208. //----------------------------------------------------------------------------------------
  209. void CClockFrame::FacetRemoved(FW_CFacet* facet)
  210. {
  211.     FW_CFrame::FacetRemoved(facet);
  212.  
  213.     this->RequestFrameShape(this->GetNewFrameShape(facet));
  214.     this->UpdateUsedAndActiveShapes();
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------
  218. // CClockFrame::FrameShapeChanged
  219. //----------------------------------------------------------------------------------------
  220. void CClockFrame::FrameShapeChanged()
  221. {
  222.     this->RequestFrameShape(this->GetNewFrameShape(this->GetActiveFacet()));
  223.     this->UpdateUsedAndActiveShapes();
  224.  
  225.     XMPShape oldShape;
  226.     this->GetFrameShape(&oldShape);
  227.     this->Invalidate(&oldShape);
  228.     this->GetXMPFrame()->InvalidateActiveBorder();
  229. }
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // CClockFrame::UpdateClock
  233. //----------------------------------------------------------------------------------------
  234. void CClockFrame::UpdateClock(unsigned long tickCount)
  235. {
  236.     FW_CFrameFacetIterator facets(this);
  237.     CClockFacet* clockFacet;
  238.     while (!facets.IsDone())
  239.     {
  240.         clockFacet = (CClockFacet*)facets.CurrentItem();
  241.         FW_CGraphicContext gc(clockFacet->GetXMPFacet());
  242.  
  243.         if (fClockPart->GetClockType() == kAnalogClock)
  244.              clockFacet->UpdateAnalogClock(&gc, tickCount);
  245.         else
  246.             clockFacet->UpdateDigitalClock(&gc, tickCount);
  247.             
  248.         facets.Next();
  249.     }
  250. }
  251.  
  252.